<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="上报时间">
        <el-date-picker
          v-model="daterangeReportingDate"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="工种类别" prop="jobCategory">
        <el-input
          v-model="queryParams.jobCategory"
          placeholder="请输入工种类别"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="姓名" prop="username">
        <el-input
          v-model="queryParams.username"
          placeholder="请输入姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!-- <el-form-item label="工单ID" prop="workorderId">
        <el-input
          v-model="queryParams.workorderId"
          placeholder="请输入工单ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item label="工作单号" prop="workorderId">
        <el-input
          v-model="queryParams.workorderId"
          placeholder="请输入工作单号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!-- <el-form-item label="工单名称" prop="workorderName">
        <el-input
          v-model="queryParams.workorderName"
          placeholder="请输入工单名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <!-- <el-form-item label="产品ID" prop="itemId">
        <el-input
          v-model="queryParams.itemId"
          placeholder="请输入产品ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item label="产品编号" prop="itemCode">
        <el-input
          v-model="queryParams.itemCode"
          placeholder="请输入产品编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="产品名称" prop="itemName">
        <el-input
          v-model="queryParams.itemName"
          placeholder="请输入产品名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!-- <el-form-item label="加工类" prop="processingCategory">
        <el-input
          v-model="queryParams.processingCategory"
          placeholder="请输入加工类"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="数量" prop="number">
        <el-input
          v-model="queryParams.number"
          placeholder="请输入数量"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="工价" prop="wages">
        <el-input
          v-model="queryParams.wages"
          placeholder="请输入工价"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="压坏" prop="crush">
        <el-input
          v-model="queryParams.crush"
          placeholder="请输入压坏"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="坏袋" prop="badBag">
        <el-input
          v-model="queryParams.badBag"
          placeholder="请输入坏袋"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="印废" prop="printingWaste">
        <el-input
          v-model="queryParams.printingWaste"
          placeholder="请输入印废"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="计件工资" prop="pieceRateWage">
        <el-input
          v-model="queryParams.pieceRateWage"
          placeholder="请输入计件工资"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="计时" prop="times">
        <el-input
          v-model="queryParams.times"
          placeholder="请输入计时"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="备注" prop="remarks">
        <el-input
          v-model="queryParams.remarks"
          placeholder="请输入备注"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="袋宽" prop="bagWidth">
        <el-input
          v-model="queryParams.bagWidth"
          placeholder="请输入袋宽"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="DZ" prop="dz">
        <el-input
          v-model="queryParams.dz"
          placeholder="请输入DZ"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['fb:handmade:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['fb:handmade:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['fb:handmade:export']"
        >导出</el-button>
      </el-col>
      <el-col :span="1.5">
          <el-button type="info" plain icon="el-icon-upload2" size="mini" @click="handleImport"
                     v-hasPermi="['mes:fb:handmade:import']">导入</el-button>
        </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="handmadeList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="年" fixed  align="center" prop="year" />
      <el-table-column label="月" fixed  align="center" prop="month" />
      <el-table-column label="日" fixed  align="center" prop="day" />
      <!-- <el-table-column label="上报时间" align="center" prop="reportingDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.reportingDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column> -->
      <el-table-column label="工种类别" align="center" fixed prop="jobCategory" />
      <el-table-column label="姓名" align="center" fixed prop="username" />
      <!-- <el-table-column label="工单ID" align="center" prop="workorderId" /> -->
      <el-table-column label="工作单号" fixed  align="center" prop="workorderId" />
      <!-- <el-table-column label="工单名称" align="center" prop="workorderName" />
      <el-table-column label="产品ID" align="center" prop="itemId" /> -->
      <el-table-column label="产品编号" align="center" prop="itemCode" />
      <el-table-column label="产品名称" align="center" prop="itemName" />
      <el-table-column label="加工类" align="center" prop="processingCategory" />
      <el-table-column label="数量" align="center" prop="number" />
      <el-table-column label="工价" align="center" prop="wages" />
      <el-table-column label="压坏" align="center" prop="crush" />
      <el-table-column label="坏袋" align="center" prop="badBag" />
      <el-table-column label="印废" align="center" prop="printingWaste" />
      <el-table-column label="报表号" align="center" prop="recordId" />
      <el-table-column label="计件工资" align="center" prop="pieceRateWage" />
      <el-table-column label="计时" align="center" prop="times" />
      <el-table-column label="备注" align="center" prop="remarks" />
      <el-table-column label="袋宽" align="center" prop="bagWidth" />
      <el-table-column label="DZ" align="center" prop="dz" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" fixed="right" >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['fb:handmade:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['fb:handmade:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改后道手工对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="1100px" append-to-body>
      <el-form ref="form" :model="form" inline :rules="rules" label-width="120px">
        <el-form-item label="年" prop="year">
          <el-input v-model="form.year" type="number" placeholder="请输入年" />
        </el-form-item>
        <el-form-item label="月" prop="month">
          <el-input v-model="form.month" type="number" placeholder="请输入月" />
        </el-form-item>
        <el-form-item label="日" prop="day">
          <el-input v-model="form.day" type="number" placeholder="请输入日" />
        </el-form-item>
        <!-- <el-form-item label="上报时间" prop="reportingDate">
          <el-date-picker clearable
            v-model="form.reportingDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择上报时间">
          </el-date-picker>
        </el-form-item> -->
        <el-form-item label="工种类别" prop="jobCategory">
          <el-input v-model="form.jobCategory" placeholder="请输入工种类别" />
        </el-form-item>
        <el-form-item label="姓名" prop="username">
          <el-input v-model="form.username" placeholder="请输入姓名" />
        </el-form-item>
        <!-- <el-form-item label="工单ID" prop="workorderId">
          <el-input v-model="form.workorderId" placeholder="请输入工单ID" />
        </el-form-item> -->
        <el-form-item label="工作单号" prop="workorderId">
          <el-input v-model="form.workorderId" type="number" placeholder="请输入工作单号" />
        </el-form-item>
        <!-- <el-form-item label="工单名称" prop="workorderName">
          <el-input v-model="form.workorderName" placeholder="请输入工单名称" />
        </el-form-item> -->
        <!-- <el-form-item label="产品ID" prop="itemId">
          <el-input v-model="form.itemId" placeholder="请输入产品ID" />
        </el-form-item> -->
        <el-form-item label="产品编号" prop="itemCode">
          <el-input v-model="form.itemCode" placeholder="请输入产品编号" />
        </el-form-item>
        <el-form-item label="产品名称" prop="itemName">
          <el-input v-model="form.itemName" placeholder="请输入产品名称" />
        </el-form-item>
        <el-form-item label="加工类" prop="processingCategory">
          <el-input v-model="form.processingCategory" placeholder="请输入加工类" />
        </el-form-item>
        <el-form-item label="数量" prop="number">
          <el-input v-model="form.number" type="number" placeholder="请输入数量" />
        </el-form-item>
        <el-form-item label="工价" prop="wages">
          <el-input v-model="form.wages" type="number" placeholder="请输入工价" />
        </el-form-item>
        <el-form-item label="压坏" prop="crush">
          <el-input v-model="form.crush" type="number" placeholder="请输入压坏" />
        </el-form-item>
        <el-form-item label="坏袋" prop="badBag">
          <el-input v-model="form.badBag" type="number"  placeholder="请输入坏袋" />
        </el-form-item>
        <el-form-item label="印废" prop="printingWaste">
          <el-input v-model="form.printingWaste" type="number"  placeholder="请输入印废" />
        </el-form-item>
        <!-- <el-form-item label="报表号" prop="reportNo">
          <el-input v-model="form.reportNo" placeholder="请输入报表号" />
        </el-form-item> -->
        
        <el-form-item label="计件工资" prop="pieceRateWage">
          <el-input v-model="form.pieceRateWage" type="number" placeholder="请输入计件工资" />
        </el-form-item>
        <el-form-item label="计时" prop="times">
          <el-input v-model="form.times" type="number" placeholder="请输入计时" />
        </el-form-item>
        <el-form-item label="备注" prop="remarks">
          <el-input v-model="form.remarks" placeholder="请输入备注" />
        </el-form-item>
        <el-form-item label="袋宽" prop="bagWidth">
          <el-input v-model="form.bagWidth" type="number" placeholder="请输入袋宽" />
        </el-form-item>
        <el-form-item label="DZ" prop="dz">
          <el-input v-model="form.dz" type="number" placeholder="请输入DZ" />
        </el-form-item>
        <!-- <el-form-item label="数据行状态(0:正常/1:删除)" prop="delFlag">
          <el-input v-model="form.delFlag" placeholder="请输入数据行状态(0:正常/1:删除)" />
        </el-form-item> -->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
     <!-- 导入对话框 -->
     <el-dialog :title="upload.title" :visible.sync="upload.open" width="400px" append-to-body>
      <el-upload ref="upload" :limit="1" accept=".xlsx, .xls" :headers="upload.headers"
                 :action="upload.url + '?updateSupport=' + upload.updateSupport" :disabled="upload.isUploading"
                 :on-progress="handleFileUploadProgress" :on-success="handleFileSuccess" :auto-upload="false" drag>
        <i class="el-icon-upload"></i>
        <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
        <div class="el-upload__tip text-center" slot="tip">
          <!-- <div class="el-upload__tip" slot="tip">
            <el-checkbox v-model="upload.updateSupport" /> 是否更新已经存在的用户数据
          </div> -->
          <span>仅允许导入xls、xlsx格式文件。</span>
          <el-link type="primary" :underline="false" style="font-size:12px;vertical-align: baseline;"
                   @click="importTemplate">下载模板</el-link>
        </div>
      </el-upload>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFileForm">确 定</el-button>
        <el-button @click="upload.open = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listHandmade, getHandmade, delHandmade, addHandmade, updateHandmade } from "@/api/mes/fb/handmade";
import {getToken} from "@/utils/auth";
import {parseTime} from "@/utils/ruoyi";

export default {
  name: "Handmade",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      daterangeReportingDate:[],
      // 导入参数
      upload: {
        // 是否显示弹出层
        open: false,
        // 弹出层标题
        title: "",
        // 是否禁用上传
        isUploading: false,
        // 是否更新已经存在的用户数据
        updateSupport: 0,
        // 设置上传的请求头部
        headers: {
          Authorization: "Bearer " + getToken()
        },
        // 上传的地址
        url: process.env.VUE_APP_BASE_API + "/mes/fb/handmade/importData"
      },
      // 总条数
      total: 0,
      // 后道手工表格数据
      handmadeList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        reportingDate: null,        jobCategory: null,        username: null,        workorderId: null,        workorderCode: null,        workorderName: null,        itemId: null,        itemCode: null,        itemName: null,        processingCategory: null,        number: null,        wages: null,        crush: null,        badBag: null,        printingWaste: null,        pieceRateWage: null,        times: null,        remarks: null,        bagWidth: null,        dz: null,      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        year: [
          { required: true, message: "年不能为空", trigger: "blur" }
        ],     
        month: [
          { required: true, message: "月不能为空", trigger: "blur" }
        ],     
        day: [
          { required: true, message: "日不能为空", trigger: "blur" }
        ],   
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
     /** 下载模板操作 */
     importTemplate() {
      this.download('/mes/fb/handmade/importTemplate', {}, `后道手工报表模板.xlsx`)
    },
    // 文件上传中处理
    handleFileUploadProgress(event, file, fileList) {
      this.upload.isUploading = true;
    },
    // 文件上传成功处理
    handleFileSuccess(response, file, fileList) {
      this.upload.open = false;
      this.upload.isUploading = false;
      this.$refs.upload.clearFiles();
      this.$alert("<div style='overflow: auto;overflow-x: hidden;max-height: 70vh;padding: 10px 20px 0;'>" + response.msg + "</div>", "导入结果", { dangerouslyUseHTMLString: true });
      this.getList();
    },
    // 提交上传文件
    submitFileForm() {
      this.$refs.upload.submit();
    },
    /** 导入按钮操作 */
    handleImport() {
      this.upload.title = "报表导入";
      this.upload.open = true;
    },
    /** 查询后道手工列表 */
    getList() {
      this.loading = true;
      this.queryParams.params = {};
      if (null != this.daterangeReportingDate && '' != this.daterangeReportingDate) {
        this.queryParams.params["beginReportingDate"] = this.daterangeReportingDate[0];
        this.queryParams.params["endReportingDate"] = this.daterangeReportingDate[1];
      }
      listHandmade(this.queryParams).then(response => {
        this.handmadeList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        recordId: null,  reportNo:null,      reportingDate: null,     year: null, month: null, day: null,      jobCategory: null,        username: null,        workorderId: null,        workorderCode: null,        workorderName: null,        itemId: null,        itemCode: null,        itemName: null,        processingCategory: null,        number: null,        wages: null,        crush: null,        badBag: null,        printingWaste: null,        pieceRateWage: null,        times: null,        remarks: null,        bagWidth: null,        dz: null,        createBy: null,        createTime: null,        updateBy: null,        updateTime: null,        delFlag: null      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangeReportingDate = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.recordId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加后道手工";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const recordId = row.recordId || this.ids
      getHandmade(recordId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改后道手工";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.recordId != null) {
            updateHandmade(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addHandmade(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const recordIds = row.recordId || this.ids;
      this.$modal.confirm('是否确认删除后道手工编号为"' + recordIds + '"的数据项？').then(function() {
        return delHandmade(recordIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.beforeExportFn();
      this.download('mes/fb/handmade/export', {
        ...this.queryParams
      }, `后道手工日报_${parseTime(new Date().getTime(), '{y}-{m}-{d}-{h}')}.xlsx`)
    }
  }
};
</script>
